<?php

namespace Api\Controller;

use Think\Controller;

//加载commons下的functions.php自定义函数库
load("@.functions");

/**
 * Description of UploadApiController
 * @author Shangyidong
 */
class UserApiController extends Controller {

    /**
     * 用户登录
     */
    function userLogin() {
        header("Content-Type: text/html; charset=utf-8");
        verify_app_key(I('post.app_key'));
        $phone = $_GET['phone'];
        $password = $_GET['password'];
//        $password = md5($password);
        if ($phone == "" || $password == "") {
//            echo '{"info":"手机号码或密码不可以为空"}';
            $result['code'] = "1010401";
            echo json_encode($result, JSON_PRETTY_PRINT);
            die();
        }
        $User = M("User"); // 实例化User对象
        $user = $User->where("phone='" . $phone . "' and password = '" . $password . "'")->find();
        if ($user == null) {
            //登录失败，请检查用户名或密码
            $result['code'] = "1010402";
            echo json_encode($result, JSON_PRETTY_PRINT);
            die();
        }
        $user['password'] = "";
        $user['token'] = create_uuid();
        $user ['last_login_time'] = date("Y-m-d H:i:s");
        $user ['count_login'] ++;
//        echo $User->getLastSql();
//        echo json_encode($user, JSON_PRETTY_PRINT);
        $result['code'] = "200";
        $result['result'] = $user;
        echo json_encode($result, JSON_PRETTY_PRINT);
        $user['password'] = $password;
        $User->where('id=' . $user['id'])->save($user); // 根据条件更新记录
    }

    /**
     * 获取单个用户信息
     */
    function get_user() {
        header("Content-Type: text/html; charset=utf-8");
        verify_app_key(I('post.app_key'));
        $id = $_GET['id'];
        if ($id == '' || !is_numeric($id) || $id <= 0) {
//            echo '{"info":"请输入正确的id"}';
            $result['code'] = "1010201";
//            $result['reason'] = "请输入正确的用户id";
            //$result['result'] = $user;
            echo json_encode($result, JSON_PRETTY_PRINT);
            die();
        }
//        echo $id;
//        $User = M("User"); // 实例化User对象
        $User = new \Common\Model\UserModel("User");
        $user = $User->where('id=' . $id)->find();
        /*
          $User ->get_user_by_token("dy_88878b32-4ea5-2617-818a-817d4a9beeeb");
          die();
         */
        $user['password'] = "";
        //echo json_encode($user, JSON_PRETTY_PRINT);
        $result['code'] = "200";
        //$result['reason'] = "success";
        $result['result'] = $user;
        echo json_encode($result, JSON_PRETTY_PRINT);
    }

    /**
     * 获取用户列表
     */
    function list_user() {
        header("Content-Type: text/html; charset=utf-8");
        verify_app_key(I('post.app_key'));
        $users = D("User");
        $total = $users->count();
        $pageSize = $_GET['pageSize'];
        if ($pageSize == '' || !is_numeric($pageSize) || $pageSize <= 0) {
            $pageSize = 10;
        }
        $page = $_GET['page'];
        /*   ///page类中有实现
          if ($page == '' || !is_numeric($page) || $page <= 0) {
          //            echo '{"info":"请输入正确的页码"}';
          //            die();
          $page = 1;
          }
         */
        $page = new \Component\Page($total, $pageSize);
        //$page->setPage($_GET['pageNumber']);
        //$page->setPage(2);
        $sql = "select * from dy_user " . $page->limit;
        $info = $users->query($sql);
        for ($i = 0; $i < count($info); $i++) {
            $info[$i]['password'] = "";
        }
//        $pageList = $page->fpage();//页码列表
//        show_bug($info);
//        $this->assign("info", $info);//将变量传递到模板
        $result['code'] = "200";
        $result['result'] = $info;
        echo json_encode($result, JSON_PRETTY_PRINT);
    }

    /**
     * 用户注册
     */
    function reg_user() {
        verify_app_key(I('get.app_key'));
        $phone = $_GET['phone'];
        $password = $_GET['password'];
        //校验数据
        if (strlen($password) < 6) {
            $result['code'] = "1010101";
            //密码长度必须大于6位
            echo json_encode($result, JSON_PRETTY_PRINT);
            die();
        }
        if (strlen($phone) < 11) {
            $result['code'] = "1010102";
            //手机号码长度不正确
            echo json_encode($result, JSON_PRETTY_PRINT);
            die();
        }
        //查询username是否可用
        $user = M("User"); //实例化User对象
        $u = $user->where("phone = '" . $phone . "'")->select();
        if ($u != null) {
            //该手机号码已注册
            $result['code'] = "1010103";
            //$result['result'] = "";
            echo json_encode($result, JSON_PRETTY_PRINT);
            die();
        }
        //向数据库中写入数据，并返回结果
        $user = D("User");
        $user->phone = $phone;
        $user->password = md5(md5($password)); //两次md5加密密码
        $user->reg_time = date("Y-m-d H:i:s");
        $user->reg_ip = "127.0.0.1";
        $rst = $user->add();
        $result['code'] = "200";

        $user = M("User"); //实例化User对象
        $u = $user->where("phone = '" . $phone . "'")->find();
        $u['password'] = "";
        $result['result'] = $u;
        echo json_encode($result, JSON_PRETTY_PRINT);
    }

    /**
     * 添加好友
     * app_key token friend_id
     */
    function add_friend() {
        verify_app_key(I('get.app_key')); //如果app_key无效则在verify_app_key函数中处理
        $user = verify_token(I('get.token')); //如果token无效则在verify_token中处理
        //继续往下执行则说明app_key和token都验证通过
        
        $friend_id = I('get.friend_id');
        $add_time = date("Y-m-d H:i:s");
        
        if(!is_int($friend_id)){
            $result['code'] = "1010501"; //无效的friend_id
            echo json_encode($result, JSON_PRETTY_PRINT);
            die();
        }
            
        $UserModel = new \Common\Model\UserModel();
        $friend_user = $UserModel->get($friend_id);
        $result['code'] = "200";
        if ($friend_user == null) {
            $result['code'] = "1010501"; //无效的friend_id
            echo json_encode($result, JSON_PRETTY_PRINT);
            die();
        }
        //检查是否已经添加过该好友
        $FriendModel = new \Common\Model\FriendModel();
        $friend = $FriendModel->where("user_id = ".$user['id']." and friend_id = ".$friend_user['id'])->find();
//        echo $FriendModel->getLastSql();
        if ($friend != null) {
            $result['code'] = "1010502"; //已经添加过该好友
            echo json_encode($result, JSON_PRETTY_PRINT);
            die();
        }
        
        if($user['id'] == $friend_user['id']){
            $result['code'] = "1010503"; //不允许添加自己为好友
            echo json_encode($result, JSON_PRETTY_PRINT);
            die();
        }

        $friend['user_id'] = $user['id'];
        $friend['friend_id'] = $friend_user['id'];
        $friend['add_time'] = $add_time;
        // 销毁单个变量
        unset($FriendModel);
        
        $FriendModel = new \Common\Model\FriendModel();
        $FriendModel->add($friend);
        echo json_encode($result, JSON_PRETTY_PRINT);
    }

    /**
     * 用户退出
     */
    function user_logout() {
        verify_app_key(I('get.app_key')); //如果app_key无效则在verify_app_key函数中处理
        $user = verify_token(I('get.token')); //如果token无效则在verify_token中处理
        //继续往下执行则说明app_key和token都验证通过
        /*
          $user['token'] = ""; //清空token
          $UserModel = new \Common\Model\UserModel();
          $UserModel->save($user);
         */
        $result['code'] = "200";
        $User = M("User");
        $r = $User->where('id = ' . $user['id'])->setField('token', '');
        if ($r !== false) {
            //echo '数据更新成功！';
        } else {
            //echo '没更新任何数据！';
        }
        echo json_encode($result, JSON_PRETTY_PRINT);
//        show_bug($result);
    }

}

?>
